import re
import xlwt
from openpyxl import Workbook
from numpy import *
import pymysql
import time
# 获取时间
from heiben_py.main.util_db import DataBaseHandle

ticks = time.time()
# 加载数据库
DbHandle = DataBaseHandle()
# 读取参数文件，将文件根据每行的空格隔开
fp1 = open(
    "D://服务器跑下来的数据//20210122 64G测试结果//mem_usage_all.log"
)
count = 0
str_list = []
h_id_s = DbHandle.selectDb_2(
    "SELECT id from machine_hardware_information where hardware_ip='10.88.66.36' limit 1"
)
id = h_id_s[0]['id']
t_id_s = DbHandle.selectDb_2(
    'select task_id from task where h_id = 14 and task_id =41')
task_id = t_id_s[0]['task_id']
data = []
sql = "INSERT INTO mem(h_id,t_id,date,kbmemfree , kbavail,kbmemused,memused,kbbuffers,kbcached,kbcommit,commit,kbactive,kbinact,kbdirty ,AMorPM) VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
for line in fp1.readlines():
    str_list = line.split()
    if str_list and len(str_list) == 13 and str_list[2] != 'kbmemfree':
        data.append((id, task_id, str_list[0], int(str_list[2]), str_list[3],
                     str_list[4], str_list[5], str_list[6], str_list[7],
                     str_list[8], str_list[9], int(str_list[10]),
                     int(str_list[11]), int(str_list[12]), str_list[1]))
if len(data) > 0:
    DbHandle.insertDBmany(sql, data)
    DbHandle.closeDb()
fp1.close()
